package dao.impl;

import dao.DeptDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.Dept;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class DeptDaoImpl implements DeptDao {
  //UserDao userDao=new UserDaoImpl();
  public List<Dept> findAll() {
      //定义Mybatis核心配置文件
      String resource = "mybatis-config.xml";
      //加载配置文件
      SqlSession sqlSession = null;
      try {
          //流操作
          InputStream inputStream = Resources.getResourceAsStream(resource);
          //创建会话工厂对象
          SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
          //创建会话对象
         sqlSession = sqlSessionFactory.openSession();
          //执行数据库操作
          List<Dept> depts = sqlSession.getMapper(DeptDao.class).findAll();
          return depts;
      } catch (IOException e) {
          throw new RuntimeException(e);
      } finally {
          sqlSession.close();
      }


  }
}
